package com.hf.lookatme.zview.fragments;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hf.lookatme.R;
import com.hf.lookatme.commons.CommonMethod;
import com.hf.lookatme.zbean.AdvertisementData;
import com.hf.lookatme.zpresenter.Present_Fragment_Main;
import com.hf.lookatme.zview.activitys.Activity_Main_Study;
import com.hf.lookatme.zview.activitys.Activity_PartTimeJob;
import com.hf.lookatme.zview.adapters.Adapter_ViewPager_Adversement;
import com.hf.lookatme.customview.ZoomOutPageTransformer;
import com.hf.lookatme.zview.interfaces.IView_Fragment_Main;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by JoeShiguang on 2015/5/15.
 */
public class Fragment_Main extends Fragment implements IView_Fragment_Main {

  private static final String TAG = "Fragment_Main";

  @InjectView(R.id.viewPager_fragment_main_advertisements)
  ViewPager viewPager_advertisements;

  private Context context;

  private View view_fragment;

  private Present_Fragment_Main present_fragment_main;

  private Adapter_ViewPager_Adversement adapter_adversement;

  private Handler handler = new Handler();


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    context = getActivity();
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view_fragment = inflater.inflate(R.layout.fragment_main, container, false);
    ButterKnife.inject(this, view_fragment);
    try {

      present_fragment_main = new Present_Fragment_Main(this);
      //获取广告信息
      present_fragment_main.getAdvertisementData(context);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return view_fragment;
  }

  @Override
  public void startAdvertisementScroll(List<AdvertisementData> list_advertisementData) {

    if (list_advertisementData != null && list_advertisementData.size() > 0) {

      if (adapter_adversement == null) {
        adapter_adversement = new Adapter_ViewPager_Adversement(context, list_advertisementData);
      }
      viewPager_advertisements.setAdapter(adapter_adversement);
      viewPager_advertisements.setPageTransformer(true, new ZoomOutPageTransformer());

      autoScrollAdvertisementDataTask.run();
    }
  }

  private Runnable autoScrollAdvertisementDataTask = new Runnable() {
    public void run() {
      if (viewPager_advertisements.getCurrentItem() < adapter_adversement.getCount() - 1) {
        viewPager_advertisements.setCurrentItem(viewPager_advertisements.getCurrentItem() + 1);
      } else {
        viewPager_advertisements.setCurrentItem(0);
      }
      handler.postDelayed(autoScrollAdvertisementDataTask, 3000);
    }
  };

  @OnClick(R.id.linearLayout_fragment_main_study)
  void onLayout_studyClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      present_fragment_main.doIntent(context, Activity_Main_Study.class);

//      Toast.makeText(context, getResources().getString(R.string.study), Toast.LENGTH_SHORT).show();
    }
  }

  @OnClick(R.id.linearLayout_fragment_main_partTimeJob)
  void onLayout_partTimeJobClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      present_fragment_main.doIntent(context, Activity_PartTimeJob.class);
    }
  }

  @OnClick(R.id.linearLayout_fragment_main_test)
  void onLayout_testClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      Toast.makeText(context, getResources().getString(R.string.test), Toast.LENGTH_SHORT).show();
    }
  }

  @OnClick(R.id.linearLayout_fragment_main_entrepreneurship)
  void onLayout_entrepreneurshipClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      Toast.makeText(context, getResources().getString(R.string.entrepreneurship), Toast.LENGTH_SHORT).show();
    }
  }

  @OnClick(R.id.linearLayout_fragment_main_jobWanted)
  void onLayout_jobWantedClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      Toast.makeText(context, getResources().getString(R.string.jobWanted), Toast.LENGTH_SHORT).show();
    }
  }

  @OnClick(R.id.linearLayout_fragment_main_consulting)
  void onLayout_consultingClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      Toast.makeText(context, getResources().getString(R.string.consulting), Toast.LENGTH_SHORT).show();
    }
  }

  @OnClick(R.id.linearLayout_fragment_main_qualificationTest)
  void onLayout_qualificationTestClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      Toast.makeText(context, getResources().getString(R.string.qualificationTest), Toast.LENGTH_SHORT).show();
    }
  }

  @OnClick(R.id.linearLayout_fragment_main_treasureHunt)
  void onLayout_treasureHuntClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      Toast.makeText(context, getResources().getString(R.string.treasureHunt), Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(autoScrollAdvertisementDataTask);
  }
}
